﻿using Microsoft.Extensions.Configuration;
using RxxAdmin.Common.Helper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace RxxAdmin.Common;

public static class AppSettings
{
    private static IConfiguration? _configuration;
    public static IConfiguration Configuration
    {
        get
        {
            if (_configuration == null) throw new NullReferenceException(nameof(Configuration));
            return _configuration;
        }
    }
    /// <summary>
    /// 设置 Configuration 的实例
    /// </summary>
    /// <param name="configuration"></param>
    /// <exception cref="Exception"></exception>
    /// <exception cref="ArgumentNullException"></exception>
    public static void AddConfigSteup(IConfiguration? configuration)
    {
        if (_configuration != null)
        {
            throw new Exception($"{nameof(Configuration)}不可修改！");
        }
        _configuration = configuration ?? throw new ArgumentNullException(nameof(configuration));
    }
    /// <summary>
    /// 封装要操作的字符
    /// </summary>
    /// <param name="sections">节点配置</param>
    /// <returns></returns>
    public static string app(params string[] sections)
    {
        try
        {
            if (sections.Any())
            {
                return Configuration[string.Join(":", sections)];
            }
        }
        catch (Exception)
        {
        }

        return "";
    }
    /// <summary>
    /// 递归获取配置信息数组
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="sections"></param>
    /// <returns></returns>
    public static List<T> app<T>(params string[] sections)
    {
        List<T> list = new List<T>();
        // 引用 Microsoft.Extensions.Configuration.Binder 包
        Configuration.Bind(string.Join(":", sections), list);
        return list;
    }
    /// <summary>
    /// 根据路径  configuration["App:Name"];
    /// </summary>
    /// <param name="sectionsPath"></param>
    /// <returns></returns>
    public static string GetValue(string sectionsPath)
    {
        try
        {
            return Configuration[sectionsPath];
        }
        catch (Exception)
        {
        }

        return "";
    }
    #region 以下存放的全部都是静态配置
    public static object Domain => Configuration["Domain"];
    public static string ApiName => Configuration["ApiName"];
    public static string AppConfigAlert => Configuration["AppConfigAlert"];
    public static class Jwt
    {
        public static string SecretKey => Configuration["Jwt:SecretKey"];
        public static string Issuer => Configuration["Jwt:Issuer"];
        public static string Audience => Configuration["Jwt:Audience"];
    }
    public static class Redis
    {
        public static bool Enabled => Configuration["Redis:Enabled"].ObjToBool();
        public static string ConnectionString => Configuration["Redis:ConnectionString"];
        public static string InstanceName => Configuration["Redis:InstanceName"];
        public static int DefaultDB => Configuration["Redis:DefaultDB"].ObjToInt();
    }
    public static class HangfireSetting {
        public static bool Enabled => Configuration["HangfireSetting:Enabled"].ObjToBool();
        public static string SqlServer => Configuration["HangfireSetting:SqlServer"];
        public static string Redis => Configuration["HangfireSetting:Redis"];
        public static string Db => Configuration["HangfireSetting:Db"];
        public static string Login => Configuration["HangfireSetting:Login"];
        public static string Password => Configuration["HangfireSetting:Password"];
    }
    public static class IpRateLimiting
    {
        public static string[] IpWhitelist => Configuration.GetSection("IpRateLimiting:IpWhitelist").Get<string[]>();
    }
    public static class Cors
    {
        public static string PolicyName => Configuration["Cors:PolicyName"];
        public static string EnableAllIPs => Configuration["Cors:EnableAllIPs"];
        public static string IPs => Configuration["Cors:IPs"];
    }
    //public static string SqlServerConnection => Configuration["SqlServerConnection"];
    public static string MainDB => Configuration["MainDB"];
    public static class DBSettings
    {
        public static string SeedDBEnabled => Configuration["DBSettings:SeedDBEnabled"];
        public static string SeedDBDataEnabled => Configuration["DBSettings:SeedDBDataEnabled"];
        public static string CachingAOP => Configuration["DBSettings:CachingAOP"];
        public static string LogToDb => Configuration["DBSettings:LogToDb"];        
    }
    public static class Middleware
    {
        public static string MiniProfiler => Configuration["Middleware:MiniProfiler"];
        public static string IpRateLimit => Configuration["Middleware:IpRateLimit"];
    }
    public static class Seq
    {
        public static string Enabled => Configuration["Seq:Enabled"];
        public static string Address => Configuration["Seq:Address"];
        public static string ApiKey => Configuration["Seq:ApiKey"];
    }
    #endregion
    /// <summary>
    /// 允许跨域请求列表
    /// </summary>
    //public static string[] AllowCors => Configuration.GetSection("AllowCors").Get<string[]>();
}
